package android.slc.box.commonlibrary.compat;

import android.slc.box.core.provider.BoxesResourceBundleWrap;
import android.slc.box.core.provider.ResourceProvider;

import java.util.ResourceBundle;

/**
 * @author slc
 * @date 2022/03/21 下午 5:30
 */
public class SlcResourceUtils {
    private ResourceBundle defResourceBundle;
    private final static SlcResourceUtils slcResourceUtils = new SlcResourceUtils();

    private SlcResourceUtils() {
        defResourceBundle = ResourceProvider.getInstance().getResourceBundle();
    }

    public static SlcResourceUtils getInstance() {
        return slcResourceUtils;
    }

    public static void setDefResourceBundle(ResourceBundle defResourceBundle) {
        getInstance().defResourceBundle = defResourceBundle;
    }

    public static ResourceBundle getDefResourceBundle() {
        return getInstance().defResourceBundle;
    }

    public static void tryAddResourceBundle(ResourceBundle defResourceBundle) {
        if(SlcResourceUtils.getDefResourceBundle() instanceof BoxesResourceBundleWrap){
            BoxesResourceBundleWrap boxesResourceBundleWrap = (BoxesResourceBundleWrap) SlcResourceUtils.getDefResourceBundle();
            boxesResourceBundleWrap.addResourceBundle(defResourceBundle);
        }
    }

    public static String getString(String key) {
        //return new String(getInstance().defResourceBundle.getString(key).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        return getString(getInstance().defResourceBundle, key);
    }

    public static String getString(ResourceBundle resourceBundle, String key) {
        //return new String(resourceBundle.getString(key).getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        return resourceBundle.getString(key);
    }
}
